/**
 * Class: OpenLayers.Strategy.SkyBBOX
 * A simple strategy that reads new features when the viewport invalidates
 *     some bounds.
 *
 * Inherits from:
 *  - <OpenLayers.Strategy.BBOX>
 */
OpenLayers.Strategy.SkyBBOX = OpenLayers.Class(OpenLayers.Strategy.BBOX, {
    
    /**
     * Property: bbox
     * {<OpenLayers.Bounds>}
     * 
     */

    bbox: null,
    
    /**
     * Method: calculateBounds
     *
     * Parameters:
     * mapBounds - {<OpenLayers.Bounds>} the current map extent, will be
     *      retrieved from the map object if not provided
     */
    calculateBounds: function(mapBounds) {
        if(!mapBounds) {
            mapBounds = this.getMapBounds();
        }
        
        var center = mapBounds.getCenterLonLat();
        
        var dataWidth = mapBounds.getWidth() * this.ratio;
        var dataHeight = mapBounds.getHeight() * this.ratio;
        this.bounds = new OpenLayers.Bounds(
            center.lon - (dataWidth / 2),
            center.lat - (dataHeight / 2),
            center.lon + (dataWidth / 2),
            center.lat + (dataHeight / 2)
        );
        
	/**
	 *  My Hack
	 */
	var map = this.layer.map;

        center = map.getCenter();
        
        if ( map.displayProjection ) {
            center = center.clone().transform( map.getProjectionObject(), map.displayProjection );
        }
        
        var size   = map.getSize();
        var res    = OpenLayers.Util.getResolutionFromScale( map.getScale(), "degrees" );

        var radius = 0.5 * Math.min( size.w, size.h ) * res * this.ratio;
              
        this.bbox = new OpenLayers.Bounds(
            center.lon,
            center.lat,
            radius,
            map.getResolution()
        );
        
    },
    
 
    /**
     * Method: createFilter
     * Creates a spatial BBOX filter. If the layer that this strategy belongs
     * to has a filter property, this filter will be combined with the BBOX 
     * filter.
     * 
     * Returns
     * {<OpenLayers.Filter>} The filter object.
     */
    createFilter: function() {
        var filter = new OpenLayers.Filter.Spatial({
            type: OpenLayers.Filter.Spatial.BBOX,
            value: this.bbox,
            projection: this.layer.projection
        });
        if (this.layer.filter) {
            filter = new OpenLayers.Filter.Logical({
                type: OpenLayers.Filter.Logical.AND,
                filters: [this.layer.filter, filter]
            });
        }
        return filter;
    },

    CLASS_NAME: "OpenLayers.Strategy.SkyBBOX" 
});
